package com.example.rudeness;

import junit.framework.TestCase;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

public class DensityHelperTest extends TestCase {

    private int num;
    private final Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();

    private static int height;
    private static int thisheight;

    public final void testRpx() {
        num = 700;
        float f = DensityHelper.rpx(num, context);
        Float rpx = rpx(num, context);
        assertEquals(rpx,f);
    }

    public void testRpx02() {
        num = 800;
        float f = DensityHelper.rpx(num, context);
        Float rpx = rpx(num, context);
        assertEquals(rpx,f);
    }

    public final void testGetDisplayWidthInPx() {
        int thiswidth = getDisplayWidthInPx(context);
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        int width = display.getAttributes().width;
        assertEquals(width, thiswidth);
    }

    public final void testGetDisplayHeightInPx() {
        thisheight = getDisplayHeightInPx(context);
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        height =  display.getAttributes().height;
        assertEquals(height, thisheight);
    }

    public final void testVp2px() {
        float vp = 100;
        Float thisVp2px = vp2px(vp, context);
        DisplayAttributes attributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
        float vp2px = (attributes.densityPixels * vp);

        assertEquals(height, thisheight);
        assertEquals(thisVp2px, vp2px);
    }


    private static final float WIDTH = 750;
    private static final float HEIGHT = 1334;

    public static float rpx(int value, Context context) {
        float wRatioValue = (getDisplayWidthInPx(context) / WIDTH) * value;
        float hRatioValue = (getDisplayHeightInPx(context) / HEIGHT) * value;
        float h = 0;
        if (value <= WIDTH) {
            h = wRatioValue;
        } else if (value > WIDTH) {
            h = hRatioValue;
        }
        return h;
    }

    public static int getDisplayWidthInPx(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display.getAttributes().width;
    }

    public static int getDisplayHeightInPx(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display.getAttributes().height;
    }

    public static float vp2px(float vp, Context context) {
        DisplayAttributes attributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
        return (attributes.densityPixels * vp);
    }



}